package db

import "time"

func (activity *Activity) Insert() (err error) {
	err = postdb.Model(activity).Create(activity).Error
	return err
}
func (activity *Activity) Update() (err error) {
	err = postdb.Model(activity).Save(activity).Error
	return err
}

// 提前结束一个活动
func FinishActivity(id int) (err error) {
	var activity Activity
	err = postdb.Model(&Activity{}).Where("id=?", id).First(&activity).Error
	if err != nil {
		return err
	}
	activity.EndTime = time.Now().Add(-1 * time.Hour)
	err = postdb.Model(activity).Save(activity).Error
	return err
}

// 这是每天调用一次的,获取到活动之后将活动列表存储在内存中
func SelectActivitys() (activitys []Activity, err error) {
	err = postdb.Where("end_time >?", time.Now()).Find(&activitys).Error
	return activitys, err
}
